package org.simp.handler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.simp.common.Constants;
import org.simp.core.message.SimpMessage;
import org.simp.core.message.SimpMessageWraper;
import org.simp.node.SimpServerAccepter;
import org.simp.protocol.core.Protocol;
import org.simp.session.SimpSession;
import org.simp.session.SimpSessionWraper;

/**
 * @author zhuhaitao at 2014-5-23
 *
 * 服务器Handler，接收消息，处理，或者转发到上层服务器
 */
public class SimpServerHandler extends AbstractSimpHandler{

	private List<SimpSession> sessions = Collections.synchronizedList(new ArrayList<SimpSession>());
	

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		SimpSession simpSession = getSession(session);
		if(simpSession != null){
			/**session关闭时，通知客户端关闭连接**/
			SimpMessageWraper msg = new SimpMessageWraper(Protocol.System.MAINID,Protocol.System.DISCONNECT);
			msg.setObjAddress(simpSession.getSessionName());
			msg.setObjAddress(context.getContextName());
			simpSession.write(msg);
			simpSession.close();
			
		}
	}

	protected SimpSession getSession(IoSession session) {
		for(SimpSession simpSession : sessions){
			if(simpSession.getIoSession() == session){
				return simpSession;
			}
		}
		return null;
	}

	@Override
	public void sessionCreated(IoSession session) throws Exception {
		SimpSession sessionWraper =getSession(session);
		if(sessionWraper == null){
			sessionWraper =new SimpSessionWraper(session);
			sessions.add(sessionWraper);
		}
	}

	@Override
	public void send(SimpMessage message) {

		SimpSession[] sessionArr = getSessionByName(message.getLastObjAddress());
		if(sessionArr != null && sessionArr.length >0 ){
			String srcAddress = message.getHead().getSrcAddress();
			String objAddress = message.getHead().getObjAddress();
			for(SimpSession session : sessionArr){
				message.setSrcAddress(srcAddress);
				message.setObjAddress(objAddress);
				message.addSrcAddress(context.getContextName());
				message.decObjAddress(context.getContextName());
				session.write(message);
			}
		}
		else{//如果找不到该地址，转发至上层服务器，在往上层服务器发送前会检查地址是否正确，不正确将抛弃消息
			context.getSimpNode().sendToUpNode(message);
		}
		
	}

	@Override
	public SimpSession getSimpSession(long sessionId) {
		for(SimpSession session : sessions){
			if(session.getSessionId() == sessionId){
				return session;
			}
		}
		return null;
	}

	@Override
	public SimpSession[] getSessionByName(String name) {
		List<SimpSession> sessionList = new ArrayList<SimpSession>();
		for(SimpSession session : sessions){
			if(name.equals(session.getSessionName()) ){
				sessionList.add(session);
			}
		}
		SimpSession [] sessionArr = new SimpSession[sessionList.size()];
		sessionList.toArray(sessionArr);
		return sessionArr;
	}

	@Override
	public SimpSession[] getSessionByType(String type) {
		List<SimpSession> sessionList = new ArrayList<SimpSession>();
		for(SimpSession session : sessions){
			if(session.getSessionType() .equals(type)){
				sessionList.add(session);
			}
		}
		SimpSession [] sessionArr = new SimpSession[sessionList.size()];
		sessionList.toArray(sessionArr);
		return sessionArr;
	}
	@Override
	public void sessionIdle(IoSession session, IdleStatus status)
			throws Exception {
		SimpSession simpSession = getSession(session);
		if(simpSession != null){
			simpSession.close();
			sessions.remove(simpSession);
		}
		
	}

	
	
}
